

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Instrumenting Jersey 2.x &mdash; Metrics</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Instrumenting Jetty" href="jetty.html" />
    <link rel="prev" title="Instrumenting JDBI" href="jdbi.html" /> 

  
  <script src="../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../index.html" class="icon icon-home"> Metrics
          

          
            
            <img src="../_static/metrics-logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                4.1
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../getting-started.html">Getting Started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="core.html">Metrics Core</a></li>
<li class="toctree-l2"><a class="reference internal" href="healthchecks.html">Health Checks</a></li>
<li class="toctree-l2"><a class="reference internal" href="ehcache.html">Instrumenting Ehcache</a></li>
<li class="toctree-l2"><a class="reference internal" href="collectd.html">Reporting to Collectd</a></li>
<li class="toctree-l2"><a class="reference internal" href="graphite.html">Reporting to Graphite</a></li>
<li class="toctree-l2"><a class="reference internal" href="httpclient.html">Instrumenting Apache HttpClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="jdbi.html">Instrumenting JDBI</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Instrumenting Jersey 2.x</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#supported-annotations">Supported Annotations</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="jetty.html">Instrumenting Jetty</a></li>
<li class="toctree-l2"><a class="reference internal" href="log4j.html">Instrumenting Log4j</a></li>
<li class="toctree-l2"><a class="reference internal" href="logback.html">Instrumenting Logback</a></li>
<li class="toctree-l2"><a class="reference internal" href="jvm.html">JVM Instrumentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="json.html">JSON Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="servlets.html">Metrics Servlets</a></li>
<li class="toctree-l2"><a class="reference internal" href="servlet.html">Instrumenting Web Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="third-party.html">Third Party Libraries</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../about/index.html">About Metrics</a></li>
<li class="toctree-l1"><a class="reference external" href="https://www.javadoc.io/doc/io.dropwizard.metrics/metrics-core/4.1.1/">Javadoc</a></li>
<li class="toctree-l1"><a class="reference external" href="https://groups.google.com/forum/#!forum/metrics-user">Mailing List</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/dropwizard/metrics">Github Page</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Metrics</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">User Manual</a> &raquo;</li>
        
      <li>Instrumenting Jersey 2.x</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/manual/jersey.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="instrumenting-jersey-2-x">
<span id="manual-jersey"></span><h1>Instrumenting Jersey 2.x</h1>
<p>Jersey 2.x changed the API for how resource method monitoring works, so a new
module <code class="docutils literal notranslate"><span class="pre">metrics-jersey2</span></code> provides <code class="docutils literal notranslate"><span class="pre">InstrumentedResourceMethodApplicationListener</span></code>,
which allows you to instrument methods on your <a class="reference external" href="https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/index.html">Jersey 2.x</a> resource classes:</p>
<p>The <code class="docutils literal notranslate"><span class="pre">metrics-jersey2</span></code> module provides <code class="docutils literal notranslate"><span class="pre">InstrumentedResourceMethodApplicationListener</span></code>, which allows
you to instrument methods on your <a class="reference external" href="https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/index.html">Jersey 2.x</a> resource classes:</p>
<p>An instance of <code class="docutils literal notranslate"><span class="pre">InstrumentedResourceMethodApplicationListener</span></code> must be registered with your Jersey
application’s <code class="docutils literal notranslate"><span class="pre">ResourceConfig</span></code> as a singleton provider for this to work.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ExampleApplication</span> <span class="kd">extends</span> <span class="n">ResourceConfig</span> <span class="o">{</span>
    <span class="o">.</span>
    <span class="o">.</span>
    <span class="o">.</span>
    <span class="n">register</span><span class="o">(</span><span class="k">new</span> <span class="n">InstrumentedResourceMethodApplicationListener</span> <span class="o">(</span><span class="k">new</span> <span class="n">MetricRegistry</span><span class="o">()));</span>
    <span class="n">config</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="na">register</span><span class="o">(</span><span class="n">ExampleResource</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
    <span class="o">.</span>
    <span class="o">.</span>
    <span class="o">.</span>
<span class="o">}</span>

<span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/example&quot;</span><span class="o">)</span>
<span class="nd">@Produces</span><span class="o">(</span><span class="n">MediaType</span><span class="o">.</span><span class="na">TEXT_PLAIN</span><span class="o">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ExampleResource</span> <span class="o">{</span>
    <span class="nd">@GET</span>
    <span class="nd">@Timed</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">show</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="s">&quot;yay&quot;</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@GET</span>
    <span class="nd">@Metered</span><span class="o">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">&quot;fancyName&quot;</span><span class="o">)</span>
    <span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/metered&quot;</span><span class="o">)</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">metered</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="s">&quot;woo&quot;</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@GET</span>
    <span class="nd">@ExceptionMetered</span><span class="o">(</span><span class="n">cause</span> <span class="o">=</span> <span class="n">IOException</span><span class="o">.</span><span class="na">class</span><span class="o">)</span>
    <span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/exception-metered&quot;</span><span class="o">)</span>
    <span class="kd">public</span> <span class="n">String</span> <span class="nf">exceptionMetered</span><span class="o">(</span><span class="nd">@QueryParam</span><span class="o">(</span><span class="s">&quot;splode&quot;</span><span class="o">)</span> <span class="nd">@DefaultValue</span><span class="o">(</span><span class="s">&quot;false&quot;</span><span class="o">)</span> <span class="kt">boolean</span> <span class="n">splode</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">splode</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">throw</span> <span class="k">new</span> <span class="n">IOException</span><span class="o">(</span><span class="s">&quot;AUGH&quot;</span><span class="o">);</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="s">&quot;fuh&quot;</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@GET</span>
    <span class="nd">@ResponseMetered</span>
    <span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/response-metered&quot;</span><span class="o">)</span>
    <span class="kd">public</span> <span class="n">Response</span> <span class="nf">responseMetered</span><span class="o">(</span><span class="nd">@QueryParam</span><span class="o">(</span><span class="s">&quot;invalid&quot;</span><span class="o">)</span> <span class="nd">@DefaultValue</span><span class="o">(</span><span class="s">&quot;false&quot;</span><span class="o">)</span> <span class="kt">boolean</span> <span class="n">invalid</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">invalid</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="n">Response</span><span class="o">.</span><span class="na">status</span><span class="o">(</span><span class="n">Response</span><span class="o">.</span><span class="na">Status</span><span class="o">.</span><span class="na">INTERNAL_SERVER_ERROR</span><span class="o">).</span><span class="na">build</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="n">Response</span><span class="o">.</span><span class="na">ok</span><span class="o">().</span><span class="na">build</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<div class="section" id="supported-annotations">
<h2>Supported Annotations</h2>
<p>Every resource method or the class itself can be annotated with &#64;Timed, &#64;Metered, &#64;ResponseMetered and &#64;ExceptionMetered.
If the annotation is placed on the class, it will apply to all its resource methods.</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">&#64;Timed</span></code> adds a timer and measures time spent in that method.</li>
<li><code class="docutils literal notranslate"><span class="pre">&#64;Metered</span></code> adds a meter and measures the rate at which the resource method is accessed.</li>
<li><code class="docutils literal notranslate"><span class="pre">&#64;ResponseMetered</span></code> adds a meter and measures rate for each class of response codes (1xx/2xx/3xx/4xx/5xx).</li>
<li><code class="docutils literal notranslate"><span class="pre">&#64;ExceptionMetered</span></code> adds a meter and measures how often the specified exception occurs when processing the resource.
If the <code class="docutils literal notranslate"><span class="pre">cause</span></code> is not specified, the default is <code class="docutils literal notranslate"><span class="pre">Exception.class</span></code>.</li>
</ul>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="jetty.html" class="btn btn-neutral float-right" title="Instrumenting Jetty" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="jdbi.html" class="btn btn-neutral" title="Instrumenting JDBI" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2010-2014, Coda Hale, Yammer Inc., 2014-2019 Dropwizard Team

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
    

  

  <script type="text/javascript" src="../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>